home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8952 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  48 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: read/write integers t
  5. Date: 7 Mar 1996 10:50:26 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4hmf1i$k1k@gail.ripco.com>
  8. NNTP-Posting-Host: cook.ripco.com
  9.  
  10. Jason Collins <jason@www.inia.net.au> in <313EBF65.4E82@www.inia.net.au>
  11. asks:
  12.  
  13. >My problem is that I'm trying to write a program that will read an integer from
  14. >the file
  15. >count.dat, increment the integer then write it back to count.dat.  I have provi
  16. >ed the listing so
  17. >that you can all tell me what I'm doing wrong.
  18.  
  19. >Thanks....and.....be gentle.
  20.  
  21. As usual, my comments and changes are marked with `/* mha - ... */'.
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. FILE *filePtr;
  27.  
  28. int /* mha - was `void' */ main()
  29. {
  30.     int /* mha - was `char' */ ctr;
  31.  
  32.     filePtr = fopen("count.dat", "rb"); /* mha - was "ab" */
  33.     fscanf(filePtr, "%d", &ctr);
  34.     fclose(filePtr);
  35.  
  36.     ctr++;
  37.  
  38.     filePtr = fopen("count.dat", "wb");
  39.     fprintf(filePtr, "%d", ctr);
  40.     fclose(filePtr);
  41.     return 0;                   /* mha - added explicit return value */
  42. }
  43.  
  44.                                                  
  45. --
  46. * Martin Ambuhl       net: mambuhl@ripco.com
  47. * Chicago, IL (USA)    
  48.